package com.yang.sso.oauth.userdetails;

import com.yang.sso.oauth.enums.UserStatusEnum;
import com.yang.sso.oauth.model.MyUserDetails;
import com.yang.sso.oauth.model.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: Islands
 * @create: 2024-04-11 20:13
 */

@Service
@RequiredArgsConstructor
public class MyUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        String pwd = passwordEncoder.encode("admin123");
        List<String> root = new ArrayList<>();
        root.add("root");
        List<String> perms = new ArrayList<>();
        perms.add("add");
        UserInfo admin = UserInfo.builder()
                .userId(1L)
                .nickname("章撒")
                .avatar("").username("admin").password(pwd).companyId(1L)
                .roles(root).perms(perms).enabled(UserStatusEnum.ONLINE).build();
        return new MyUserDetails(admin);
    }
}
